function parseLine(_result,_name,_line){
    var line=_line.replace(RegExp("%","g"),"").split(":")[1].replace(RegExp(" ","g"),"")
        _result[_name]={}
        var line_items=line.split(",")

        for (var i=0, item=line_items[i]; i<line_items.length; item=line_items[++i]) {
            var value=parseFloat(item)
            if (value==0&&item.indexOf(".")!=-1) {
                value = "0.0"
            }
            var name=item.replace(value,"").replace(".0","")
            _result[_name][name]=parseFloat(value)
        }//for
}//parseLine

function parseProces(_result,_line){
    var items = _line.split(",");
    var processes = {
            pid: items[0], // 进程id
            name: items[1], // 进程名称
            thread: items[4], // 线程数
            user: items[29], // 用户
            cpu: items[2], // %CPU
            mem: items[9], // MEM
            time: items[3], // 进程启动以来运行时间
            command: items[1], // 进程指令
    }
    _result.processes.push(processes)
}//parseProces

function parse(data, { pid_limit }){
    if (!data) {
        return;
    }
    var result = {
        processes:[]
    };
    var data_line = data.split("\n")
    // console.log('data_line+++++', data_line);
//sys info
//parseLine("top",data_line[0])
// parseLine(result,"task",data_line[1])
// parseLine(result,"cpu",data_line[2].replace(" us,","user,").replace(" sy,"," system,").replace(" id,"," idle,"))
//console.dir(data_line[2])
//console.dir(data_line[3])
// parseLine(result,"ram",data_line[3].replace(RegExp("k ","g")," ") )
// parseLine(result,"swap",data_line[4].replace("free.","free,"))	

//process
if(pid_limit){
    if(pid_limit >= data_line.length-1){
        pid_limit = data_line.length-1;
    }else{
        pid_limit += 7;
    }
} else { // if pid_limit
    pid_limit = data_line.length - 1;
}
for(var i=7,item=data_line[i]; i<pid_limit; item = data_line[++i]){
    if(item){
    var line = item.replace(/\s{1,}/g, ',').substring(1)
        if(line!=""){
            parseProces(result,line)
        }//if
    }//if item
            }//for process
result.time=new Date().getTime()				

return result
}//parse


module.exports = parse;